﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.UI.WebControls;
using Hematology.Data;
using Hematology.HmtMembership;
using Hematology.Tools;
using Hematology.UserSite.Controls;
using NgoResource = Hematology.UserSite.App_GlobalResources.UserSite;

namespace Hematology.UserSite
{
	public partial class Trainings : NgoPage
	{
		protected EnumHelper.ContentType CurrentContentType
		{
			get
			{
				return ViewState["CurrentContentType"] != null
						   ? (EnumHelper.ContentType)Enum.Parse(typeof(EnumHelper.ContentType), ViewState["CurrentContentType"].ToString())
						   : EnumHelper.ContentType.Nothing;
			}
			set { ViewState["CurrentContentType"] = value; }
		}

		private SiteContent GetLastContent(EnumHelper.ContentType contentType)
		{
			return SiteContent.ListAllContent((int)contentType, !MembershipHelper.MembershipService.IsAuthorizedRequest, true).FirstOrDefault();
		}

		private void FillViewStateVariables()
		{
			string contentType = Request.QueryString["ct"];
			if (Array.IndexOf(Enum.GetNames(typeof(EnumHelper.ContentType)), contentType) > -1)
			{
				CurrentContentType =
					(EnumHelper.ContentType)Enum.Parse(typeof(EnumHelper.ContentType), contentType);
			}
		}

		private void FillSummaryInfo()
		{
			List<SiteContent> lTrainings = new List<SiteContent>
			                               	{
			                               		GetLastContent(EnumHelper.ContentType.Career),
			                               		GetLastContent(EnumHelper.ContentType.Education),
			                               		GetLastContent(EnumHelper.ContentType.Grant),
			                               		GetLastContent(EnumHelper.ContentType.OtherResources)
			                               	};

			lTrainings.RemoveAll(siteContent => siteContent == null);

			rptTrainingsFull.DataSource = lTrainings;
			rptTrainingsFull.DataBind();
		}

		protected string ConvertToType(object contentTypeCode)
		{
			return Enum.GetName(typeof(EnumHelper.ContentType), contentTypeCode);
		}

		private void FillContentInfoList(int contentTypeCode, Repeater targetRepeater)
		{
			List<SiteContent> lTrainings = SiteContent.ListAllContent(contentTypeCode, !MembershipHelper.MembershipService.IsAuthorizedRequest, true);

			targetRepeater.DataSource = lTrainings;
			targetRepeater.DataBind();
		}

		private void FillContentList()
		{
			if (CurrentContentType == EnumHelper.ContentType.Nothing)
			{
				FillSummaryInfo();
			}
			else
			{
				FillContentInfoList((int)CurrentContentType, rptTrainingsFull);
			}
			mvTrainings.ActiveViewIndex = 0;
		}

		private void FillContentProperties()
		{
			if (CurrentContentType == EnumHelper.ContentType.Nothing)
			{
				lblTrainingsListType.Text = NgoResource.resTopicTrainings;
				ptTitle.Text = NgoResource.resTopicTrainings;

				mvTrainingsType.ActiveViewIndex = 0;
			}
			else
			{
				int iCurrentContentType = (int)CurrentContentType;

				SiteContentType siteContentType = SiteContentType.GetSiteContentTypeByCode(iCurrentContentType);
				if (siteContentType != null)
				{
					lblTrainingsListType.Text = siteContentType.Name;
					ptTitle.Text = siteContentType.Name;
				}

				FillContentInfoList(iCurrentContentType, rptTrainings);

				mvTrainingsType.ActiveViewIndex = 1;
			}
		}


		private void FillContentInfo()
		{
			string sSiteContentId = Request.QueryString["id"];
			int iSiteContentId;
			if (!String.IsNullOrEmpty(sSiteContentId) && int.TryParse(sSiteContentId, out iSiteContentId))
			{
				ptTitle.NavigateUrl = String.Format("/Trainings.aspx?ct={0}", CurrentContentType);
				SiteContent oSiteContent = SiteContent.Get(iSiteContentId);
				if (oSiteContent != null)
				{
					List<SiteContent> oSiteContentList = new List<SiteContent>
                                                                 {
                                                                     oSiteContent
                                                                 };

					fvTrainings.DataSource = oSiteContentList;
					fvTrainings.DataBind();

					mvTrainings.ActiveViewIndex = 1;
				}
			}
			else
			{
				if (CurrentContentType != EnumHelper.ContentType.Nothing)
					ptTitle.NavigateUrl = "/Trainings.aspx";
				FillContentList();
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				FillViewStateVariables();
				FillContentProperties();
				FillContentInfo();
			}
		}

		protected void rptContentAttach_ItemCommand(object source, RepeaterCommandEventArgs e)
		{
			if (e.CommandName == "OpenAttach")
			{
				int iAttachId = Convert.ToInt32(e.CommandArgument);
				FileAttach fileAttach = FileAttach.Get(iAttachId);
				if (fileAttach != null)
				{
					Response.Redirect(Path.Combine(ConfigReader.SiteContentAttachmentPath, fileAttach.DocumentName));
				}
			}
		}
	}
}